/**
 *  ClassName: Encrypt.java
 *  created on 2011-12-14  上午10:00:44
 *  Copyrights 2011-2012 magtech All rights reserved.
 */
package com.zm.study.common;

import java.security.MessageDigest;

/**
 * @author jl
 */
public class Encrypt {
	/**
	 * 方法说明:加密字符串
	 * @param sourceString 源字符串
	 * @return 加密后的字符串
	 */
	private static String encode(String sourceString) {
		String resultString = null;
		try {
			resultString = new String(sourceString);
			MessageDigest md = MessageDigest.getInstance("MD5");
			//进行加密
			byte[] b = md.digest(resultString.getBytes("UTF-8"));
			resultString = byte2hexString(b);
		} catch (Exception ex) {
		}
		return resultString;
	}

	/**
	 * 方法说明:把字节数组转换成字符串.
	 * @param bytes
	 * @return
	 */
	private static String byte2hexString(byte[] bytes) {
		StringBuilder buf = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buf.toString();
	}
	/**
	 * 加密字符串
	 * @param str
	 * @return
	 */
	public static String encrypt(String str, String str1) {
		if(str != null && !"".equals(str)){
			String result = encode("magtech" + str);
			StringBuffer sb = new StringBuffer();
			for(int i = result.length() - 1; i >= 0; i --) {
				sb.append(result.substring(i, i +1));
			}
			result = encode(sb + str1);
			sb = new StringBuffer();
			for(int i = result.length() - 1; i >= 0; i --) {
				sb.append(result.substring(i, i +1));
			}
			result = encode(sb + "journalx3");
			sb = new StringBuffer();
			for(int i = result.length() - 1; i >= 0; i --) {
				sb.append(result.substring(i, i +1));
			}
			return sb.toString();
		} else {
			return null;
		}
		
	}

	public static void main(String[] args) {
		System.out.println(Encrypt.encrypt("Magtech测试","1005-1116"));
	}
	
}
